<?php
class PostController extends BaseController {
	
	protected function _init_() {
		$this->addTab(__('撰写文章'), 'post.write');
		$this->addTab(__('文章列表'), 'post.index');
		$this->addTab(__('文章分类'), 'post.category');
	}
	
	/**
	 * 文章列表
	 */
	public function indexAction($params = array()) {
		$page = max(1, intval($params['page']));
		$cateid = intval($params['cateid']);
		$userid = intval($params['userid']);
		$status = $params['status'];
		$order  = in_array($params['order'], array('id','views','comments')) ? $params['order'] : '';
		$ascdesc  = $params['ascdesc'] == 'asc' ? 'asc' : 'desc'; 
		$keyword  = String::safeStr($params['$keyword']);
		$pagesize = 20;
		
		$query = array();
		$wheresql = "`type` = 'blog'";
		//按分类查找
		if ($cateid > 0) {
			$wheresql .= " AND cateid = '{$cateid}'";
			$query['cateid'] = $cateid;
		}
		//按用户查找
		if ($userid > 0) {
			$wheresql .= " AND userid = '{$userid}'";
			$query['userid'] = $userid;
		}
		//按状态查找
		if ($status !== '') {
			$wheresql .= " AND `status` = '{$status}'";
			$query['status'] = $status;
		}
		//按关键字查找
		if (!empty($keyword)) {
			$wheresql .= " AND title LIKE '%{$keyword}%'";
			$query['keyword'] = $keyword;
		}
		//排序
		if (!empty($order)) {
			$ordersql = "{$order} {$ascdesc}";
			$query['order'] = $order;
			$query['ascdesc'] = $ascdesc;
		} else {
			$ordersql = "id DESC";
		}

		$count = $this->db->count($this->db->getTable('post'), $wheresql);
		$list = array();
		if ($count > 0) {
			$sql = "SELECT id,cateid,title,userid,username,views,comments,dateline,`status`,password " .
					"FROM ".$this->db->getTable('post')." WHERE {$wheresql} {$ordersql}";
			$list = $this->db->getList($sql, $page, $pagesize);
		}
		
		$pager = new Pager($page, $count, $query);
		$this->assign('pageBar', $pager->makeHtml());
		$this->assign($query);
		$this->assign('list', $list);
		
		$this->display();
	}
	
	/**
	 * 撰写/编辑文章
	 */
	public function writeAction($params = array()) {
		if ($this->input->isPost()) {
			$this->save();
		} else {
			$post = array('commentstatus'=>1);
			//热门标签
			$hottags = '';
			$tags = $this->db->getList("SELECT * FROM ".$this->db->getTable('tag')." ORDER BY `count`", 1, 50);
			foreach ($tags as $tag) {
				$hottags .= '<a href="javascript:;">'.$tag['tagname'].'</a>';
			}
			//编辑
			if ($params['id'] > 0) {
				$post = M('Post', intval($params['id']));
				$post->toArray();
			}
			$datetime = getdate($post['dateline'] ? $post['dateline'] : NOW);
			Editor::init();
		}
		$this->display();
	}
	
	private function save() {
		$id = intval($this->input->id);
		$dateline = mktime($this->input->hour, $this->input->minute, $this->input->second, $this->input->month, $this->input->day, $this->input->year);
		if ( !$dateline ) $dateline = NOW;
		$yearmonth = JBlog::date($dateline, 'Ym');
		$day = JBlog::date($dateline, 'j');
		
		$post = M('Post', $id);
		$post->title = $this->input->title;
		$post->cateid = $this->input->cateid;
		$post->type = 'blog';
		$post->parentid = intval($this->input->parentid);
		$post->alias = $this->input->alias;
		$post->password = $this->input->password;
		$post->content = $this->input->content;
		$post->alias = $this->input->alias;
		$post->dateline = $dateline;
		
		//检查分类
		$cate = M('Cate', $post->cateid);
		if (!$cate->exists || $cate->type != $post->type) {
			$this->prompt(__('分类不存在'));
		}
		if ($cate->children > 0) {
			$this->prompt(__('该分类下还有子分类，请先删除或移动子分类再删除父分类'));
		}
		if ($post->exists) {
			$post->userid = $this->userid;
			$post->username = $this->user->username;
		}
		if (!$post->exists) {
			$ret = $post->add();
		} else {
			$ret = $post->save();
		}
		if (!$ret) {
			$this->prompt($post->error);
		} else {
			//新增日历记录
			if ($post->isNew) {
				$cal = M('Calendar');
				$cal->yearmonth = $yearmonth;
				$cal->day = $day;
				$cal->postid = $post->id;
				$cal->add();
			} else {
				$cal = M('Calendar', $post->id);
				$cal->yearmonth = $yearmonth;
				$cal->day = $day;
				$cal->postid = $post->id;
				$cal->save();
			}
			//检查文章附件
			$attachments = JBlog::cookie('upload_attachments');
			if ( !empty($attachments) && preg_match('/^[0-9]+(,[0-9]+)+?$/', $attachments) ) {
				$this->db->execute("UPDATE ".$this->db->getTable('attach')." SET postid = '$id' WHERE id IN ($attachments)");
				JBlog::cookie('upload_attachments', '');
			}
			//跳转到列表
			$this->redirect(URL('post.index'));
		}
	}

}
?>
